"""
1. 访问
[首页> 基金数据 > 每日净值: 开放式基金](https://fund.10jqka.com.cn/datacenter/jz/)
2. 拦截Step1请求的 https://fund.10jqka.com.cn/data/Net/info/all_rate_desc_0_0_1_9999_0_0_0_jsonp_g.html 获取所有开放式基金

from addons.x10jqka.kaifangshi_fund import KaifangshiFund
route_mappings = {
    r".*all_rate_desc_0_0_1_9999_0_0_0_jsonp_g.*": KaifangshiFund()
}
"""

import json
from mitmproxy import http, websocket
import logging

from db import DB


class KaifangshiFund:
    def __init__(self):
        self.num = 0

    async def request(self, flow: http.HTTPFlow, db: DB):
        self.num = self.num + 1
        logging.info(f"\n\n*****\nKaifangshiFund.request** We've seen {self.num} flows, flow.id= {flow.id}, db= {db.srrldb.client_state} \n***\n")
        
        await db.save_flow_request(flow)


    def response(self, flow: http.HTTPFlow, db: DB):
        self.num = self.num + 1
        # 获取响应头的内容类型值
        content_type = flow.response.headers.get("content-type")
        logging.info(f"\n\n*****\nKaifangshiFund.response** self.num: {self.num}, flow.id= {flow.id}, flow.response: {flow.response} content_type={content_type}\n")
        # Check if the response object exists and has content
        if flow.response and len(flow.response.content) > 0:
            # pretty_url = flow.request.pretty_url() # can not call flow.request in response, we need to use flow.id
            # Get the Content-Type header
            content_type = flow.response.headers.get("content-type", "")

            # Parse the charset from the Content-Type header
            charset = "utf-8"  # Default to UTF-8 if not specified
            if "charset=" in content_type:
                charset = content_type.split("charset=")[-1].strip()
            
            # Check if the content is likely to be text-based
            if "text/" in content_type or "application/json" in content_type:
                try:
                    # Decode the content using the specified charset
                    text_content = flow.response.content.decode(charset)
                    # Now you can work with the text_content as a string

                    # # Remove the function call wrapper to isolate the JSON string g({})
                    # json_start = text_content.index('{')
                    # json_end = text_content.rindex('}') + 1
                    # json_string = text_content[json_start:json_end]

                    # # Parse the JSON string
                    # data = json.loads(json_string)

                    # # Access the "count" field
                    # count = data['data']['info']['count']

                    logging.info("text_content len=%d %s ", len(
                        text_content), text_content[:100] )
                except UnicodeDecodeError:
                    logging.info(
                        f"Failed to decode content using charset: {charset}")
            else:
                print("Non-text content, skipping decoding.")
        logging.info("*********")


addons = [KaifangshiFund()]
